﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace Pacman.Simulator
{
    class InfluenceMap
    {
        int x;
        public const int width = 28;
        public const int height = 31;

        public int[,] influenceMapArray = new int[width, height];

        public InfluenceMap()
        {
            x = 0;
        }


        // Exports the current values of the influence map as a csv file (openable in excel).
        public void exportCSV(string path)
        {

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path))
            {
                string line = "";

                for (int y = 0; y < height; y++)
                {

                    line += influenceMapArray[0, y];
                    for (x = 1; x < width; x++)
                    {
                        line += ",";
                        line += influenceMapArray[x, y];

                    }
                    file.WriteLine(line);
                    line = "";

                }

            }

        }


        // Sets the value of the influence map to be those of the CSV file at 'path'
        public void loadCSV(string path)
        {

            String[,] values = parseCSV(path);

            for (int i = 0; i < width; i++)
                for (int j = 0; j < height; j++)
                {
                    influenceMapArray[i, j] = Convert.ToInt32(values[i, j]);
                }
            return;
        }


        // Helper function of the 'loadCSV' function.
        // Code modified from http://www.switchonthecode.com/tutorials/building-a-simple-csv-parser-in-csharp
        public string[,] parseCSV(string path)
        {
            string[,] parsedData = new string[width, height];

            try
            {
                using (StreamReader readFile = new StreamReader(path))
                {
                    string line;
                    string[] row;

                    for (int y = 0; y < height; y++)
                    {
                        line = readFile.ReadLine();
                        row = line.Split(',');
                        for (int x = 0; x < row.Length; x++)
                            parsedData[x, y] = row[x];
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return parsedData;
        }

    }
}
